<template>
  <section class="demo-section">
    <div class="demo-header">
      <h2 class="demo-title">2. defineCustomElement 自定义元素 (Vue 3.2+)</h2>
      <p class="demo-description">
        defineCustomElement 允许你将 Vue 组件转换为原生自定义元素（Web Components）。
        这使得 Vue 组件可以在任何框架中使用，甚至在原生 HTML
        中直接使用，提供了极佳的跨框架兼容性。
      </p>
    </div>

    <div class="demo-content">
      <div class="controls">
        <div class="control-group">
          <label>计数器名称:</label>
          <input
            v-model="counterName"
            type="text"
            placeholder="输入自定义名称"
            class="text-input"
          />
        </div>
        <div class="control-group">
          <label>初始值:</label>
          <input v-model.number="initialValue" type="number" class="number-input" />
        </div>
        <button @click="createCustomElement" class="btn btn-primary">
          创建自定义元素
        </button>
      </div>

      <div class="custom-element-demo">
        <h4>自定义元素展示:</h4>
        <div class="element-container" ref="elementContainer"></div>

        <div class="code-example">
          <h5>使用方式:</h5>
          <pre><code>&lt;!-- 在任何 HTML 页面中使用 --&gt;
&lt;my-vue-counter-[timestamp] 
  name="{{ counterName || 'Vue计数器' }}" 
  initial="{{ initialValue }}"&gt;
&lt;/my-vue-counter-[timestamp]&gt;

&lt;!-- 或者通过 JavaScript 创建 --&gt;
const element = document.createElement('my-vue-counter-[timestamp]');
element.setAttribute('name', '{{ counterName || 'Vue计数器' }}');
element.setAttribute('initial', '{{ initialValue }}');
document.body.appendChild(element);</code></pre>
        </div>
      </div>
    </div>
  </section>
</template>

<script setup lang="ts">
import { ref, defineCustomElement } from "vue";

// defineCustomElement 相关状态
const counterName = ref("Vue计数器");
const initialValue = ref(0);
const elementContainer = ref<HTMLElement>();

// 创建自定义元素
function createCustomElement() {
  if (!elementContainer.value) return;

  // 清空容器
  elementContainer.value.innerHTML = "";

  // 定义 Vue 组件
  const MyCounter = {
    props: ["name", "initial"],
    setup(props: any) {
      const count = ref(parseInt(props.initial) || 0);
      const increment = () => count.value++;
      const decrement = () => count.value--;

      return { count, increment, decrement };
    },
    template: `
      <div style="
        padding: 20px; 
        border: 2px solid #667eea; 
        border-radius: 12px; 
        background: linear-gradient(135deg, #f8fafc 0%, #e2e8f0 100%);
        text-align: center;
        font-family: system-ui;
        max-width: 300px;
        margin: 10px auto;
      ">
        <h3 style="color: #2c3e50; margin: 0 0 15px 0;">{{ name }}</h3>
        <div style="font-size: 2rem; font-weight: bold; color: #667eea; margin: 15px 0;">
          {{ count }}
        </div>
        <div style="display: flex; gap: 10px; justify-content: center;">
          <button @click="decrement" style="
            padding: 8px 16px; 
            background: #ef4444; 
            color: white; 
            border: none; 
            border-radius: 6px; 
            cursor: pointer;
            font-weight: 600;
          ">-1</button>
          <button @click="increment" style="
            padding: 8px 16px; 
            background: #10b981; 
            color: white; 
            border: none; 
            border-radius: 6px; 
            cursor: pointer;
            font-weight: 600;
          ">+1</button>
        </div>
      </div>
    `,
  };

  // 定义自定义元素
  const CustomCounter = defineCustomElement(MyCounter);

  // 生成唯一的元素名称，避免重复定义
  const elementName = `my-vue-counter-${Date.now()}`;
  customElements.define(elementName, CustomCounter);

  // 创建元素实例
  const element = document.createElement(elementName);
  element.setAttribute("name", counterName.value);
  element.setAttribute("initial", initialValue.value.toString());

  elementContainer.value.appendChild(element);
}
</script>

<style scoped>
.demo-section {
  background: white;
  margin-bottom: 3rem;
  border-radius: 16px;
  padding: 2rem;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.05);
}

.demo-header {
  margin-bottom: 2rem;
}

.demo-title {
  font-size: 1.8rem;
  font-weight: 600;
  color: #2c3e50;
  margin-bottom: 1rem;
}

.demo-description {
  font-size: 1rem;
  color: #64748b;
  line-height: 1.7;
  background: #f8fafc;
  padding: 1rem;
  border-radius: 8px;
  border-left: 4px solid #667eea;
}

.demo-content {
  display: grid;
  gap: 2rem;
}

.controls {
  display: flex;
  flex-wrap: wrap;
  gap: 1rem;
  align-items: center;
  padding: 1rem;
  background: #f1f5f9;
  border-radius: 8px;
}

.control-group {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.control-group label {
  font-size: 0.9rem;
  font-weight: 600;
  color: #374151;
}

.text-input,
.number-input {
  padding: 0.5rem;
  border: 2px solid #e2e8f0;
  border-radius: 6px;
  transition: border-color 0.3s ease;
  min-width: 150px;
}

.text-input:focus,
.number-input:focus {
  outline: none;
  border-color: #667eea;
}

.btn {
  padding: 0.75rem 1.5rem;
  border: none;
  border-radius: 8px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s ease;
}

.btn-primary {
  background: #667eea;
  color: white;
}

.btn-primary:hover {
  background: #5a67d8;
  transform: translateY(-2px);
}

.custom-element-demo {
  display: grid;
  gap: 1rem;
}

.element-container {
  min-height: 100px;
  border: 2px dashed #e2e8f0;
  border-radius: 8px;
  padding: 1rem;
  display: flex;
  align-items: center;
  justify-content: center;
}

.code-example {
  background: #1e293b;
  color: #e2e8f0;
  padding: 1rem;
  border-radius: 8px;
  font-family: "Monaco", monospace;
}

.code-example h5 {
  color: #f1f5f9;
  margin: 0 0 0.5rem 0;
}

.code-example pre {
  margin: 0;
  background: none;
}

@media (max-width: 768px) {
  .controls {
    flex-direction: column;
    align-items: stretch;
  }

  .control-group {
    width: 100%;
  }
}
</style>
